<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 商品套装操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 * create by allen.qiang
 */

require 'module/base/promotionHandler.class.php';

class pa_package {
	//入口
	public function execute(){
		global $G_PGLOB;
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		switch ($operFlg){
			case 1:
				$this->init();
				break;
			case 2:
			case 3:
				$this->showEdit();
				break;
			case 4:
				$this->showInfo();
				break;
			default:
				$G_PGLOB->smarty->assign('error_msg','参数错误');
				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
				break;
		}
	}
	//初始化列表页
	private function init (){
		global $G_PGLOB;
		$G_PGLOB->smarty->display('promotion/packageList.tpl');
	}
	//新增、编辑套装页初始化
	private function showEdit (){
		global $G_PGLOB;
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		$flg = isset($_REQUEST['flg']) ? $_REQUEST['flg'] : null;//(邮购或网站)
		$goods_group_id = isset($_REQUEST['goods_group_id']) ? $_REQUEST['goods_group_id'] : null;
				
		$G_PGLOB->smarty->assign('flg', $flg);
		if ($operFlg == 2){
			//显示新增
			$G_PGLOB->smarty->assign('operFlg', 1);
		}else {
			//显示编辑
			$G_PGLOB->smarty->assign('operFlg', 2);
			
			/*------------------表单部分start-------------------------*/
			$commHandler = new commHandler($G_PGLOB->DBCA);
			
			$arg_array = array('goods_group_id'=>$goods_group_id);
			//查询得到主商品
			$result_main = $commHandler->search($arg_array, 'promotion_group');
			
			$postID = '';
			
			if (!empty($result_main)){
				$G_PGLOB->smarty->assign('groupInfo', $result_main[0]);
				$postID = $result_main[0]->postID;
				$product_code = $result_main[0]->product_code;
			}
			/*------------------表单部分end-------------------------*/
			
// 			$param_arr = array('goods_group_id'=>$goods_group_id);
			
// 			$result_goods = $commHandler->search($param_arr, 'goods_list');
			
// 			$mainPostID = empty($result_goods) ? '' : $result_goods[0]->mainPostID;
// 			//主商品信息
// 			$mainGoodsInfo = array($product_code,$postID,$mainPostID);
// 			$mainGoodsInfo = json_encode($mainGoodsInfo);
// 			$G_PGLOB->smarty->assign('mainGoodsInfo', $mainGoodsInfo);
			
			$promotionHandler = new promotionHandler($G_PGLOB->DBCA);
			
			$importData = array('goods_group_id'=>$goods_group_id);
			//得到套装商品
			$result_detail = $promotionHandler->searchGroupDetailAll($importData);

            $detail_info = array();
			if ($result_detail['code'] == 200 && !empty($result_detail['message']['data'])){
				$detail_info = $result_detail['message']['data'];
			}
            //主商品数据
            $main_goods = array();
            foreach ($detail_info as $key => $goods){
                if($goods->product_code == $product_code){
                    $main_goods = $goods;
                }
            }

			$G_PGLOB->smarty->assign('main_goods', $main_goods);
			$G_PGLOB->smarty->assign('groupGoodsInfo', $detail_info);
		}
		$G_PGLOB->smarty->display('promotion/packageDetail.tpl');
	}
	//显示详情页
	private function showInfo (){
		global $G_PGLOB;
		$goods_group_id = isset($_REQUEST['goods_group_id']) ? $_REQUEST['goods_group_id'] : null;
		
		$commHandler = new commHandler($G_PGLOB->DBCA);
		
		$arg_array = array('goods_group_id'=>$goods_group_id);

		$result_search = $commHandler->search($arg_array, 'promotion_group');
		
		if (empty($result_search)){
			$G_PGLOB->smarty->assign('error_msg','该套餐不存在或已删除，请刷新页面');
		}else {
			$G_PGLOB->smarty->assign('groupInfo',$result_search[0]);
		}

        //得到套装商品
        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);
        $result_detail = $promotionHandler->searchGroupDetailAll($arg_array);

        $detail_info = array();
        if ($result_detail['code'] == 200 && !empty($result_detail['message']['data'])){
            $detail_info = $result_detail['message']['data'];
        }
        //主商品数据
        $main_goods = array();
        foreach ($detail_info as $key => $goods){
            if($goods->product_code == $result_search[0]->product_code){
                $main_goods = $goods;
            }
        }

        $G_PGLOB->smarty->assign('main_goods', $main_goods);
        $G_PGLOB->smarty->assign('groupGoodsInfo', $detail_info);


		$G_PGLOB->smarty->display('promotion/packageInformation.tpl');
	}
	
}